﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using shop.service;
using shop.entity;
using shop.exception;
using shop.util;
using shop.handler;

namespace shop.handler
{
    /// <summary>
    /// OrderHandler 的摘要说明
    /// </summary>
    public class OrderHandler : IHttpHandler
    {
        private OrderService orderService = new OrderService();
        private GoodsService goodsService = new GoodsService();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string method = context.Request["method"];
         
            if (method == null || method.Trim().Equals(""))
            {
                //toDo
                printErrorJson(context, "method not exist");
                return;
            }
            method =  method.Trim();
           if (method.Equals("add"))
            {
                Add(context);
            }
            else if (method.Equals("update"))
            {
                Update(context);
            }
            else if (method.Equals("delete"))
            {
                Delete(context);
            }
            else {
                printErrorJson(context, "method not error");               
            }         
        }

        private void Add(HttpContext context) {
           int userId = int.Parse(context.Request["userId"]);
            int goodsId = int.Parse(context.Request["goodsId"]);

            Order order = new Order() {
                UserId = userId,
                GoodsId = goodsId,
                OrderTime = DateTime.Now.ToString()

            };
            string json;
            try
            {
                orderService.Add(order);

                json = JsonUtil.getJsonString(JsonResult<string>.RenderSuccessMessage("下单成功！"));
            }
            catch (ShopParamException e)
            {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(1, e.ErrorMessage));
            }
            catch (Exception e) {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(1, e.Message));
            }
            context.Response.Write(json);
        }
        private void Update(HttpContext context)
        {
            int userId =RequestUtil.ParseStringToInt(context.Request, "userId");
            int goodsId = RequestUtil.ParseStringToInt(context.Request, "goodsId");
            int orderId = RequestUtil.ParseStringToInt(context.Request, "orderId");
            int catagoryId = RequestUtil.ParseStringToInt(context.Request, "catagoryId");
            string userName = context.Request["userName"];
            string description = context.Request["description"];
            string goodsName = context.Request["goodsName"];
            Double price = RequestUtil.ParseStringToDouble(context.Request, "catagoryId");

            Order order = new Order()
            {
                Id = orderId,
                UserId = userId,
                GoodsId = goodsId,
                OrderTime = DateTime.Now.ToString(),
                PayStatus = 1,
               
                
            };
            Goods goods = new Goods() {
                Price = price,
                CategoryId =catagoryId,
                Name = userName,
                Description = description
            };

            string json;
            try
            {
                orderService.UpdateOrder(order);
                goodsService.UpdateGoods(goods);
                json = JsonUtil.getJsonString(JsonResult<string>.RenderSuccessMessage("更新订单成功！"));
            }
            catch (ShopParamException e)
            {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(1, e.ErrorMessage));
            }
            catch (Exception e)
            {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(1, e.Message));
            }
            context.Response.Write(json);
        }

        private void Delete(HttpContext context)
        {
            int id = int.Parse(context.Request.QueryString["id"]);         
            string json;
            try
            {
                orderService.DeleteOrder(id);

                json = JsonUtil.getJsonString(JsonResult<string>.RenderSuccessMessage("删除品成功！"));
            }
            catch (ShopParamException e)
            {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(1, e.ErrorMessage));
            }
            catch (Exception e)
            {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(1, e.Message));
            }
            context.Response.Write(json);
        }

        private void printErrorJson(HttpContext context,string error) {
            context.Response.Write(JsonUtil.getJsonString(JsonResult<string>.RenderError(1,error)));
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


    }
}